package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



import cn.jhc.bean.PubliShers;
import cn.jhc.factory.ConnectionFactory;

public class PubliShersDAO {
	public List<PubliShers> getAll() {
		List<PubliShers> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "select  publisherID,publisherName"
				+ " from PubliShers";
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				PubliShers PubliShers = new PubliShers();
				PubliShers.setPublisherID(rs.getInt(1));
				PubliShers.setPublisherName(rs.getString(2));
				list.add(PubliShers);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	public boolean addPubliShers(PubliShers publiShers) {
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "insert into publishers(publisherName) values('"
				+ publiShers.getPublisherName() + "')";
		System.out.println(sql);
		Statement statement = null;

		try {
			statement = connection.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return true;
	}

	public PubliShers findById(int PublisherID) {
		PubliShers publiShers = new PubliShers();
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		final String sql = "select  PublisherID,PublisherName,zipcode,phone,creditcard,"
				+ "total from PubliShers where PublisherID = ?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, PublisherID);
			rs = ps.executeQuery();
			if (rs.next()) {
				publiShers.setPublisherID(rs.getInt(1));
				publiShers.setPublisherName(rs.getString(2));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return publiShers;
	}

	public void DeleteById(int PublisherID) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		final String sql = "delete from publishers where publisherID = ?";
		System.out.println(sql);
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, PublisherID);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {

				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

}
